package com.oocl.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.exception.MyException;
import com.oocl.pojo.Muser;
import com.oocl.security.SHASecurity;
import com.oocl.security.SecretKeys;
import com.oocl.service.MuserService;
import com.oocl.service.impl.MuserServiceImpl;


public class UpdateFromAServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String icnum = request.getParameter("icnum");
		String token = request.getParameter("token");
		String status1 =  request.getParameter("status1");
		status1 = status1.trim();
		icnum =icnum.trim();
		token = token.trim();
		System.out.println(status1);
		System.out.println("token:"+token);
		MuserService mSer = new MuserServiceImpl();
		try {
			Muser muser = mSer.loadMuserByIcnum(icnum);
			String tokenSreKeys = "";
			synchronized(SecretKeys.class){
				tokenSreKeys = SHASecurity.getSha1(muser.getToken()+SecretKeys.getSecretKeys());
				System.out.println("tokenSreKeys:"+tokenSreKeys);
			}
			if(tokenSreKeys.equals(token)){
				muser.setStatus1(status1);
				int count = mSer.updateMuserStatus1(muser);
				if(count>0){
					response.getWriter().write("200 Ok");
				}else{
					response.sendRedirect("exception.jsp");
					return;
				}
			}else{
				   response.getWriter().write("500 no exist token");
			}
		} catch (MyException e) {
			throw e;
		}
	}

}
